package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Adres;
import hibernate.model.Kullanici;
import hibernate.model.Musteri;
import hibernate.service.EmmyService;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("MusteriBean")
@Scope("session")
public class MusteriBean implements BeanService, Serializable  {

	@Autowired
	private transient EmmyService emmyService;
	
	private Musteri currentMusteri;
	private Musteri selectedMusteri;
	private List<Musteri> musteriList;	
	private String tcKontrolText;
	private String klcAdKontrolText;
	private String DgTarihBitis;
	

	@Override
	@PostConstruct
	public void init(){		
		
	}	
	
	@Override
	public String restart(){
		init();
		return "list";
	}
	
	@Override
	public String deleteView(){
		if(emmyService.delete(currentMusteri.getKullanici())){
			return "ok";			
		}else{				
				return "hata";
		}
	}

	@Override
	public String viewNew() {
		System.out.println("new User");
		currentMusteri = new Musteri();
		currentMusteri.setAdres(new Adres());
		currentMusteri.setKullanici(new Kullanici());		
		currentMusteri.getKullanici().setGrup(1);
		currentMusteri.getAdres().setGrupid(1);
		return "musteriKayit";
	}

	@Override
	public String viewDetail() {/*
		List<Musteri> valueList = (List<Musteri>) musteriTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = musteriTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentMusteri= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentMusteri.getKullaniciid());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ return"detay";
	}
	
	public void TcKontrol() {						
		try{
			Musteri mus =  (Musteri) emmyService.
					 getQuery("findMusteritcKimlik", "mTcKimlik", currentMusteri.getTcKimlik()).get(0); 
			if(mus != null){
				tcKontrolText = "TC kimlik daha önceden kayıtlı";
				currentMusteri.setTcKimlik(0L);
			}				
		}catch(java.lang.IndexOutOfBoundsException ioe){
			System.out.println("Tc Kimlik kayıtlı değil");
			tcKontrolText = "OK";
		}
		catch(Exception e){
			tcKontrolText = e.toString();
		}
	}
	
	public void kullaniciAdKontrol(){
		try{			
			System.out.println("1");
			System.out.println("::: ="+currentMusteri.getKullanici().getKullaniciAdi() );
			System.out.println("text : "+klcAdKontrolText);
			Kullanici klc = (Kullanici) emmyService.
					 getQuery("findKlcKulAdi", "kKulAdi", ""+currentMusteri.getKullanici().getKullaniciAdi()).get(0);
			System.out.println("2");
			if(klc != null){
				klcAdKontrolText = "Kullanıcı Adı daha önceden kayıtlı";
				System.out.println("3");
				currentMusteri.getKullanici().setKullaniciAdi("");
				System.out.println("4");
			}	
		}catch(java.lang.IndexOutOfBoundsException ioe){
			System.out.println("Kullanıcı Adı kayıtlı değil");
			klcAdKontrolText = "OK";
		}
		catch(Exception e){
			klcAdKontrolText = e.toString();
		}
	}
	@Override
	public String SaveOrUpdate() throws MyException {		
			if(emmyService.saveOrUpdate(currentMusteri)){				
				return "OK";
			}else{				
				return "ERR";
			}		
	}
	
	public void save(ActionEvent actionEvent) throws MyException {  
		 FacesMessage msg;
	        if(SaveOrUpdate().equals("OK")){
	        	msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Process Completed...");  
		        FacesContext.getCurrentInstance().addMessage(null, msg);
	        }else{
	        	msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "unsuccessful", "İşlem Başarısız, Tekrar deneyiniz.");  
		        FacesContext.getCurrentInstance().addMessage(null, msg);
	        }
	}

	@SuppressWarnings("unchecked")
	public String getDetayliMusteri(){
		String query = createQuery();
		if(!query.equals("list")){
			musteriList = (List<Musteri>) emmyService.getQuery(createQuery());
		}		
		return "list";
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public String createQuery() {
		Musteri mus  = currentMusteri;		
		String query = "from Musteri";				
		
		if(mus == null){
			return query;
		}
		if(mus.getTcKimlik() != 0){
			musteriList = (List<Musteri>) emmyService
					.getQuery("findMusteritcKimlik","mTcKimlik",mus.getTcKimlik());
			return "list";
		}
		if(mus.getAd() != null){
			if(query.indexOf("where") != -1){
				query += " and ad like '%"+mus.getAd()+"%'";
				
			}
			else{
				query += " where ad like '%"+mus.getAd()+"%'";			
			}
		}
		if(mus.getSoyad() != null){
			if(query.indexOf("where") != -1){
				query += " and soyad like '%"+mus.getSoyad()+"%'";
				
			}
			else{
				query += " where soyad like '%"+mus.getSoyad()+"%'";			
			}
		}
		if(mus.getTel1() != 0){
			if(query.indexOf("where") != -1){
				query += " and tel1="+mus.getTel1()+"";
				
			}
			else{
				query += " where tel1="+mus.getTel1()+"";			
			}
		}
		if(mus.getTel2() != 0){
			if(query.indexOf("where") != -1){
				query += " and tel2="+mus.getTel2()+"";
				
			}
			else{
				query += " where tel2="+mus.getTel2()+"";			
			}
		}
		if(mus.getFax() != 0){
			if(query.indexOf("where") != -1){
				query += " and fax="+mus.getFax()+"";
				
			}
			else{
				query += " where fax="+mus.getFax()+"";			
			}
		}
		if(mus.getEmail() != null){
			if(query.indexOf("where") != -1){
				query += " and email like '%"+mus.getEmail()+"%'";
				
			}
			else{
				query += " where email like '%"+mus.getEmail()+"%'";			
			}
		}
		if(mus.getKisiselWebAdd() != null){
			if(query.indexOf("where") != -1){
				query += " and kisiselWebAdd like '%"+mus.getKisiselWebAdd()+"%'";
				
			}
			else{
				query += " where kisiselWebAdd like '%"+mus.getKisiselWebAdd()+"%'";			
			}
		}
		if(mus.getDogumTarihi() != null){
			if(query.indexOf("where") != -1){
				query += " and (dogumTarihi between "+mus.getDogumTarihi()+" and "+DgTarihBitis+")";
				
			}
			else{
				query += " where (dogumTarihi between "+mus.getDogumTarihi()+" and "+DgTarihBitis+")";			
			}
		}
		return query;
	}

	@Override
	public void getSession() {
		// TODO Auto-generated method stub
		
	}

	public Musteri getCurrentMusteri() {
		return currentMusteri;
	}

	public void setCurrentMusteri(Musteri currentMusteri) {
		this.currentMusteri = currentMusteri;
	}

	public Musteri getSelectedMusteri() {
		return selectedMusteri;
	}

	public void setSelectedMusteri(Musteri selectedMusteri) {
		this.selectedMusteri = selectedMusteri;
	}

	public List<Musteri> getMusteriList() {
		return musteriList;
	}

	public void setMusteriList(List<Musteri> musteriList) {
		this.musteriList = musteriList;
	}

	public String getTcKontrolText() {
		return tcKontrolText;
	}

	public void setTcKontrolText(String tcKontrolText) {
		this.tcKontrolText = tcKontrolText;
	}

	public String getKlcAdKontrolText() {
		return klcAdKontrolText;
	}

	public void setKlcAdKontrolText(String klcAdKontrolText) {
		this.klcAdKontrolText = klcAdKontrolText;
	}

	public String getDgTarihBitis() {
		return DgTarihBitis;
	}

	public void setDgTarihBitis(String dgTarihBitis) {
		DgTarihBitis = dgTarihBitis;
	}
}